package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpPageRequest;
import com.itheima.pojo.PageResponseResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    //注入
    @Autowired
    private EmpService empService;

    //员工列表查询
    @GetMapping
    public Result queryAllEmp(EmpPageRequest empPageRequest) {
        PageResponseResult<Emp> prr=empService.queryAllEmp(empPageRequest);
        log.info("请求参数为:{}",empPageRequest);
        return Result.success(prr);
    }

    //新增员工
    @PostMapping
    public Result insertEmp(@RequestBody Emp emp) {
        empService.insertEmp(emp);
        log.info("插入的emp是{}",emp);
        return Result.success();
    }

    //修改员工信息,根据id查询回显
    @GetMapping("/{id}")
    public Result queryByIdEmp(@PathVariable Integer id) {
        Emp emp = empService.queryByIdEmp(id);
        log.info("利用id查询到的emp是{}",emp);
        return Result.success(emp);
    }

    //修改员工信息,修改信息
    @PutMapping
    public Result updateEmp(@RequestBody Emp emp) {
        log.info("要修改的emp的信息是:{}",emp);
        empService.updateEmp(emp);
        return Result.success();
    }

    //删除员工
    @DeleteMapping
    public Result deleteEmp(@RequestParam List<Integer> ids) {
        log.info("数组的参数{}", ids.toString());
        empService.deleteEmp(ids);
        return Result.success();
    }

//    查询所有员工
    @GetMapping("/list")
    public Result selectEmps() {
        List<Emp> emps = empService.selectEmps();
        return Result.success(emps);
    }
}
